package com.cm.cl;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Dlg_Setting extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtLocalhost;
	private JTextField txtPort;
	private JTextField txtRoot;
	private JTextField txtPwd;
	private JTextField txtScheme;
	private JTextField txtTableName;

	

	/**
	 * Create the dialog.
	 */
	public Dlg_Setting() {
		setLocationRelativeTo(null);
		setTitle("\u8FDE\u63A5\u8BBE\u7F6E");
		setBounds(100, 100, 312, 357);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		txtLocalhost = new JTextField();
		txtLocalhost.setText("localhost");
		txtLocalhost.setBounds(106, 26, 111, 21);
		contentPanel.add(txtLocalhost);
		txtLocalhost.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("\u4E3B\u673A");
		lblNewLabel.setBounds(29, 29, 54, 15);
		contentPanel.add(lblNewLabel);
		
		JLabel label = new JLabel("\u7528\u6237\u540D");
		label.setBounds(29, 106, 54, 15);
		contentPanel.add(label);
		
		JLabel label_1 = new JLabel("\u5BC6\u7801");
		label_1.setBounds(29, 146, 54, 15);
		contentPanel.add(label_1);
		
		JLabel label_2 = new JLabel("\u7AEF\u53E3");
		label_2.setBounds(29, 67, 54, 15);
		contentPanel.add(label_2);
		
		txtPort = new JTextField();
		txtPort.setText("3306");
		txtPort.setBounds(106, 64, 111, 21);
		contentPanel.add(txtPort);
		txtPort.setColumns(10);
		
		txtRoot = new JTextField();
		txtRoot.setText("root");
		txtRoot.setBounds(106, 103, 111, 21);
		contentPanel.add(txtRoot);
		txtRoot.setColumns(10);
		
		txtPwd = new JTextField();
		txtPwd.setBounds(106, 143, 111, 21);
		contentPanel.add(txtPwd);
		txtPwd.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("\u6570\u636E\u5E93\u540D\u79F0");
		lblNewLabel_1.setBounds(10, 188, 73, 15);
		contentPanel.add(lblNewLabel_1);
		
		txtScheme = new JTextField();
		txtScheme.setText("test");
		txtScheme.setBounds(106, 185, 111, 21);
		contentPanel.add(txtScheme);
		txtScheme.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("表名称");
		lblNewLabel_2.setBounds(10, 225, 54, 15);
		contentPanel.add(lblNewLabel_2);
		
		txtTableName = new JTextField();
		txtTableName.setText("worktable");
		txtTableName.setBounds(106, 222, 111, 21);
		contentPanel.add(txtTableName);
		txtTableName.setColumns(10);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			
			JButton btnNewButton = new JButton("\u6D4B\u8BD5\u8FDE\u63A5");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Database.setHost(txtLocalhost.getText());
					Database.setUsername(txtRoot.getText());
					Database.setPassword(txtPwd.getText());
					Database.setPort(txtPort.getText());
					Database.setDbName(txtScheme.getText());
					Database.setTableName(txtTableName.getText());
					if(Database.getConnection() != null){
						JOptionPane.showMessageDialog(null, "连接成功");
					}else{
						JOptionPane.showMessageDialog(null, "连接失败");
					}
					
				}
			});
			buttonPane.add(btnNewButton);
			{
				JButton okButton = new JButton("确定");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						Database.setHost(txtLocalhost.getText());
						Database.setUsername(txtRoot.getText());
						Database.setPassword(txtPwd.getText());
						Database.setPort(txtPort.getText());
						Database.setDbName(txtScheme.getText());
						Database.setTableName(txtTableName.getText());
						Database.getConnection();
						Dlg_Setting.this.dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
	}
}
